package com.itheima.skyserver.service.impl;

import com.itheima.skyserver.exception.PasswordNotMatchException;
import com.itheima.skyserver.exception.UsernameNotExistException;
import com.itheima.skyserver.mapper.EmployeeMapper;
import com.itheima.skyserver.pojo.domain.Employee;
import com.itheima.skyserver.service.EmployeeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Objects;

@Slf4j
@Service
public class EmployeeServiceImpl implements EmployeeService {


    @Autowired
    private EmployeeMapper employeeMapper;

    /**
     * 员工登录
     * @param employee
     * @return
     */
    @Override
    public Employee checkLogin(Employee employee) {
        //1.查询用户名是否存在
        Employee loginEmp = employeeMapper.selectByUsername(employee.getUsername());

        if(loginEmp==null){
            throw new UsernameNotExistException("用户名不存在");
        }

        //2.验证密码是否正确
        if(!Objects.equals(loginEmp.getPassword(),employee.getPassword())){
            throw  new PasswordNotMatchException("密码错误");
        }

        //3.返回结果
        return loginEmp;
    }
}
